package com.moter.demo01.streamcaculator;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class CreateStream {
    public static void main(String[] args) throws IOException {
        //数组创建
        String content = "moter wyq wx";
        String[] s = content.split(" ");
        List<String> list = Arrays.asList(s);
        Stream<String> stream1 = Arrays.stream(s);
//        Stream<String> stream2 = Arrays.stream(s,1,5);
        //list创建
//        Stream<String> stream1 = list.stream();
//        Stream<String> stream1 = list.parallelStream();

        //stream.of创建
        Stream<String> name = Stream.of("moteroter", "wyoq");
        //通过Stream.generate(消费者接口) 无限流
        Stream<String> generate = Stream.generate(() -> UUID.randomUUID().toString());
        //通过Stream.empty()
        Stream<String> empty = Stream.empty();
        //Stream.iterate(T seed,UnaryOperator<T> f) //依次加一
        Stream<String> iterate = Stream.iterate("1", n -> n + "1");

        //Paths.lines(Path p,StandardCharsets.UTF_8)
        Stream<String> lines = Files.lines(Paths.get("src\\main\\java\\com\\moter\\demo01\\streamcaculator\\client.txt"), StandardCharsets.UTF_8);
        Stream<String> a = Pattern.compile("[abc]").splitAsStream("aqwehb21dces");
//        for (Object o : a.toArray()) {
//            System.out.println(o);
//            // qweh
////            21d
////            es
//        }

        //练习 fliter map flatMap方法
//        name.filter(n->n.contains("mo")).forEach(System.out::println);
//        name.map(n->n=n.length()+"").forEach(System.out::println);
//        Stream<String> stringStream = name.flatMap(n -> Arrays.stream(n.split("o")));
//        stringStream
//                .skip(1)//跳过前n个
//                .distinct()//去重
//                .limit(3)//提取前n个
//                .forEach(System.out::println);
        //使用连接流来创建流
        Stream<String> concat = Stream.concat(stream1, iterate);
        concat.limit(10)
//                .sorted(Comparator.comparingInt(String::length))//排序
                .sorted()//原生排序
                .peek(n-> System.out.println(n.length())).forEach(System.out::println);

    }
}
